package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireProprietorModel;
import com.fire.web.modules.all.mapper.FireProprietorMapper;
import com.fire.web.modules.all.mapper.SequenceMapper;
import com.fire.web.modules.all.request.AddProprietorReq;
import com.fire.web.modules.all.service.IFireProprietorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业主单位Service
 *
 * @author Matthew
 * @Date 2019-01-10 10:46
 */
@Service
public class FireProprietorServiceImpl extends ServiceImpl<FireProprietorMapper, FireProprietorModel> implements IFireProprietorService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireProprietorServiceImpl.class);

	@Autowired
	private FireProprietorMapper fireProprietorMapper;
	@Autowired
	private SequenceMapper sequenceMapper;


	@Override
	public FireProprietorModel selectOne(FireProprietorModel entity) {
		return fireProprietorMapper.selectOne(entity);
	}

	@Override
	public List<FireProprietorModel> selectList(FireProprietorModel model) {
		return fireProprietorMapper.selectList(model, new EntityWrapper<FireProprietorModel>());
	}

	@Override
	public List<FireProprietorModel> selectList(FireProprietorModel model, Wrapper<FireProprietorModel> wrapper) {
		return fireProprietorMapper.selectList(model, wrapper);
	}

	@Override
	public List<FireProprietorModel> selectPage(Pagination pagination, FireProprietorModel model, Wrapper<FireProprietorModel> wrapper) {
		return fireProprietorMapper.selectPage(pagination, model, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addProprietor(AddProprietorReq req, String adminid) {
		FireProprietorModel fireProprietorModel = new FireProprietorModel();
		String id = sequenceMapper.getNextStringValue("proprietor");
		fireProprietorModel.setProprietor(id);
		fireProprietorModel.setCommanyname(req.getCommanyname());
		fireProprietorModel.setCommanyphone(req.getCommanyphone());
		fireProprietorModel.setCompanyaddress(req.getCompanyaddress());
		fireProprietorModel.setContactname(req.getContactname());
		fireProprietorModel.setCreatetime(new Date());
		fireProprietorModel.setUpdatetime(fireProprietorModel.getCreatetime());
		fireProprietorModel.setEmail(req.getEmail());
		fireProprietorModel.setOwner(req.getOwner());
		fireProprietorModel.setPhone(req.getPhone());
		fireProprietorModel.setWeb(req.getWeb());
		fireProprietorModel.setAdminid(adminid);
		fireProprietorMapper.insert(fireProprietorModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "新增成功", "新增成功");
	}

	@Override
	public AjaxResult updateProprietor(AddProprietorReq req) {
		FireProprietorModel fireProprietorModel = new FireProprietorModel();
		fireProprietorModel.setCommanyname(req.getCommanyname());
		fireProprietorModel.setCommanyphone(req.getCommanyphone());
		fireProprietorModel.setCompanyaddress(req.getCompanyaddress());
		fireProprietorModel.setContactname(req.getContactname());
		fireProprietorModel.setUpdatetime(new Date());
		fireProprietorModel.setEmail(req.getEmail());
		fireProprietorModel.setOwner(req.getOwner());
		fireProprietorModel.setPhone(req.getPhone());
		fireProprietorModel.setWeb(req.getWeb());
		fireProprietorModel.setSupervisorid(req.getSupervisorid());
		fireProprietorModel.setSupervisorname(req.getSupervisorname());
		fireProprietorMapper.update(fireProprietorModel, new EntityWrapper<FireProprietorModel>().eq("proprietor", req.getProprietor()));
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "更新成功", "更新成功");
	}

	@Override
	public AjaxResult getProprietorItem(){
		List<Map> proprietorItem = fireProprietorMapper.getProprietorItem();
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "获取下拉选成功", proprietorItem, "获取下拉选成功");
	}

}
